Imports KanBan.Pgd.Presentation.Controllers
Imports KanBan.Pgd.Presentation.Views
Imports KanBan.Pgd.Infrastructure.Notifications
Imports KanBan.Pgd.Presentation.Session

Public Class ShowNotifications

    Private Const DefaulGridViewRowHeight As Integer = 20

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Dim userId As Guid = ClientSession.UserCredentials.Id

        Dim notifications As IList(Of Notification) = Notifier.GetAllNotifications(userId)

        Me.AddNotifications(notifications)

    End Sub

#Region "IShowNotificationsView"

    ''' <summary>
    ''' Agrega notificaciones a la grilla
    ''' </summary>
    ''' <param name="notifications"></param>
    ''' <remarks></remarks>
    Public Sub AddNotifications(ByVal notifications As IEnumerable(Of Notification))

        For Each n As Notification In notifications
            AddNotification(n)
        Next

    End Sub

#End Region

#Region "Event Handling"

    Private Sub lnkSelectAll_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)

        For Each r As DataGridViewRow In grvNotifications.Rows

            r.Cells(NotificationsGridColumns.IsChecked).Value = True

        Next

    End Sub

    Private Sub lnkSelectRead_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)

        For Each r As DataGridViewRow In grvNotifications.Rows

            Dim isRead As Boolean = CType(r.Cells(NotificationsGridColumns.IsRead).Value, Boolean)

            If isRead Then
                r.Cells(NotificationsGridColumns.IsRead).Value = True
            End If

        Next

    End Sub

    Private Sub btnDeleteSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim notificationsToDelete As New List(Of Guid)

        For Each r As DataGridViewRow In grvNotifications.Rows

            Dim isSelected As Boolean = CType(r.Cells(NotificationsGridColumns.IsChecked).Value, Boolean)

            If isSelected Then
                Dim notificationId As Guid = New Guid(r.Cells(NotificationsGridColumns.Id).Value.ToString())
                notificationsToDelete.Add(notificationId)
            End If

        Next

        Me.DeleteNotifications(notificationsToDelete)

    End Sub

    Public Sub DeleteNotifications(ByVal notificationIds As IList(Of Guid))


        Notifier.DeleteNotifications(notificationIds)


    End Sub

    Public Sub ShowNotification(ByVal notificationId As Guid)

        Dim form As New ShowNotification(notificationId)
        form.Show()


    End Sub

    Private Sub lblSelectNone_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)

        For Each r As DataGridViewRow In grvNotifications.Rows

            r.Cells(NotificationsGridColumns.IsChecked).Value = False

        Next

    End Sub

    Private Sub grvNotifications_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

        If e.RowIndex >= 0 Then

            Dim notificationId As Guid = CType(grvNotifications.Rows(e.RowIndex).Cells(NotificationsGridColumns.Id).Value, Guid)

            Me.ShowNotification(notificationId)

        End If

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

#End Region

#Region "Private"

    Private Sub AddNotification(ByVal notification As Notification)

        Dim row As New DataGridViewRow()
        row.CreateCells(grvNotifications)
        row.Height = DefaulGridViewRowHeight

        row.Cells(NotificationsGridColumns.Id).Value = notification.Id
        row.Cells(NotificationsGridColumns.IsChecked).Value = False
        row.Cells(NotificationsGridColumns.IsRead).Value = notification.IsRead
        row.Cells(NotificationsGridColumns.Title).Value = notification.Title
        row.Cells(NotificationsGridColumns.Message).Value = notification.Message

        grvNotifications.Rows.Add(row)

    End Sub

    Private Enum NotificationsGridColumns
        IsChecked = 0
        IsRead = 1
        Image = 2
        Id = 3
        Title = 4
        Message = 5
    End Enum

#End Region

End Class